package com.yekaile.carsystem.util;

/**
 * @author yekaile
 */
public class ClientInfoUtil {

    /**
     * 根据User-Agent，得到用户浏览器和操作系统信息
     *
     * @param userAgentInfo
     * @return ClientInfo
     */
    public static ClientInfo getClientInfo(String userAgentInfo) {
        String info = userAgentInfo.toUpperCase();
        ClientInfo clientInfo = new ClientInfo();
        String[] strInfo = info.substring(info.indexOf("(") + 1,
                info.indexOf(")") - 1).split(";");
        if ((info.indexOf("MSIE")) > -1) {
            clientInfo.setBrowserInfo(strInfo[1].trim());
            clientInfo.setOsInfo(strInfo[2].trim());
        } else {
            String[] str = info.split(" ");
            if (info.indexOf("NAVIGATOR") < 0 && info.indexOf("FIREFOX") > -1) {
                clientInfo.setBrowserInfo(str[str.length - 1].trim());
                clientInfo.setOsInfo(strInfo[2].trim());
            } else if ((info.indexOf("OPERA")) > -1) {
                clientInfo.setBrowserInfo(str[0].trim());
                clientInfo.setOsInfo(strInfo[0].trim());
            } else if (info.indexOf("CHROME") < 0
                    && info.indexOf("SAFARI") > -1) {
                clientInfo.setBrowserInfo(str[str.length - 1].trim());
                clientInfo.setOsInfo(strInfo[2].trim());
            } else if (info.indexOf("CHROME") > -1) {
                clientInfo.setBrowserInfo(str[str.length - 2].trim());
                clientInfo.setOsInfo(strInfo[2].trim());
            } else if (info.indexOf("NAVIGATOR") > -1) {
                clientInfo.setBrowserInfo(str[str.length - 1].trim());
                clientInfo.setOsInfo(strInfo[2].trim());
            } else {
                clientInfo.setBrowserInfo("Unknown Browser");
                clientInfo.setOsInfo("Unknown OS");
            }
        }
        return clientInfo;
    }
}
